package ToYesterday.Day10_queue;
//test
public class implementInterface {
}

class QueueNode {
    int val;
    QueueNode next;

    public QueueNode(int val) {
        this.val = val;
    }

    public QueueNode() {
        this.val = 0;
    }
}

interface InterfaceQueue {
    void push(int element);

    // define an interface for pop method
    /* write your code here */
    int pop();

    // define an interface for top method
    /* write your code here */
    int top();
}


class MyQueue implements InterfaceQueue {
    QueueNode dummy;
    QueueNode rear;

    /* you can declare your private attributes here */
    public MyQueue() {
        dummy = new QueueNode();
        rear = dummy;

        // do initialization if necessary
    }

    // implement the push method
    /* write your code here */
    @Override
    public void push(int val) {
        QueueNode nNode = new QueueNode(val);
        rear.next = nNode;
        rear = rear.next;
    }

    // implement the pop method
    /* write your code here */
    @Override
    public int pop() {
        int val=0;
        if (dummy.next != null) {
            val = dummy.next.val;
            dummy.next = dummy.next.next;
        }
        if (dummy.next == null) {
            rear = dummy;
        }
        return val;
    }

    // implement the top method
    /* write your code here */
    @Override
    public int top() {
        return dummy.next.val;

    }
}

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue queue = new MyQueue();
 * queue.push(123);
 * queue.top(); will return 123;
 * queue.pop(); will return 123 and pop the first element in the queue
 */